package TUILayer;
import ControllerLayer.*;
import java.util.Scanner;

/**
 * Write a description of class SalesmanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SalesmanUI
{
    // instance variables - replace the example below with your own
    private SalesmanController smCtr;

    /**
     * Constructor for objects of class SalesmanUI
     */
    public SalesmanUI()
    {
        // initialise instance variables
        smCtr = SalesmanController.getInstance();
    }

    public void start()
    {
        Menu();
    }

    public void Menu()
    {
        boolean exit = false;

        while(!exit)  //if exit = true, the main menu will shut down 
        {
            int choice = writeMainMenu();
            switch(choice)
            {
                case 1:
                create();
                break;
                case 2:
                read();
                break;
                case 3:
                update();
                break;                
                case 4:
                delete();
                break;
                case 5:
                printID();
                break;
                case 6:
                printAll();
                break;
                default:                
                exit = true;
                break; 
            }
        }
    }

    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ****Salesman Menu***");
        System.out.println("  (1) Create");
        System.out.println("  (2) Read");
        System.out.println("  (3) Update");
        System.out.println("  (4) Delete");
        System.out.println("  (5) Print ID");
        System.out.println("  (6) Print ALL");
        System.out.println("  (7) Return");
        System.out.println("\n make your choice: ");

        int choice = keyboard.nextInt();

        return choice;

    }

    private long inputID()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        long out = 0;        
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type salesman id: " );
        while(!ok)
        {
            try{
                out = keyboard.nextLong();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
        return out;
    }  

    private int inputPhoneNo()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int out = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type phone number: " );
        while(!ok)
        {   
            try{
                out = keyboard.nextInt();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }    

        return out;
    }

    private String inputName()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type name: " );
        String out = keyboard.nextLine();
        return out;
    }

    private String inputAddress()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type address: " );
        String out = keyboard.nextLine();
        return out;
    }

    private String inputMail()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type mail: " );
        String out = keyboard.nextLine();
        return out;
    }

    
    private void create()
    {
        Scanner keyboard = new Scanner(System.in);    
        
        smCtr.createSalesman(inputName(), inputAddress(), inputMail(), inputPhoneNo());
        long id = smCtr.getID();
        System.out.println("    new Salesman has been created - se info above");       
        smCtr.printOutSalesmanID(id);
        System.out.println(" Salesman code nr.: "+smCtr.readSalesman(id));
        
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();

    }

    public void read()
    {
        Scanner keyboard = new Scanner(System.in); 

        long id = inputID();

        if(smCtr.readSalesman(id) != null)
        {
            System.out.println("    Salesman was found - se info above");       
            smCtr.printOutSalesmanID(id);
            System.out.println(" Salesman code nr.: "+smCtr.readSalesman(id));
        }
        else
        {
            System.out.println("       Salesman was not found - try another ID");
        }

        System.out.println();  

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void update()
    {        
        Scanner keyboard = new Scanner(System.in);
        long id = inputID();

        if(smCtr.readSalesman(id) != null)
        {
            smCtr.updateSalesman(id, inputName(), inputAddress(), inputMail(), inputPhoneNo());

            System.out.println("       Salesman has been updated - se info above");
            smCtr.printOutSalesmanID(id);
            System.out.println(" Salesman code nr.:"+smCtr.readSalesman(id));
        }
        else
        {
            System.out.println("       Salesman was not found - try another ID");
        }
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void delete()
    {
        Scanner keyboard = new Scanner(System.in);

        long id = inputID();       

        if(smCtr.readSalesman(id) != null)
        {
            System.out.println("         Salesman is deleted - se info above");
            smCtr.printOutSalesmanID(id);
            smCtr.deleteSalesman(id);
        }
        else
        {
            System.out.println("         Salesman was not found  - try another ID ");
        }
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printID()
    {
        Scanner keyboard = new Scanner(System.in); 

        long id = inputID();

        if(smCtr.readSalesman(id) != null)
        {
            System.out.println("    Salesman was found - se info:");       
            smCtr.printOutSalesmanID(id);
            System.out.println(" Salesman code nr.: "+smCtr.readSalesman(id));
        }
        else
        {
            System.out.println("       Salesman was not found - try another ID");
        } 

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();       
        
    }

    public void printAll()
    {
        Scanner keyboard = new Scanner(System.in);
        
        if(smCtr.getSize() > 0)
        {
            System.out.println("    Show list of all Salesmen - se info:");
            smCtr.printOutAllSalesman();
        }
        else
        {
            System.out.println("         No Salesman was found  - create a salesman ");
        }
        
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

}
